/*
* Send ajax and get promises for future use*/

import axios from 'axios'

export default function ajax(url,data={},type='GET') {

  return new Promise((resolve, reject)=>{
    let promise
    if(type==='GET'){
      promise = axios.get(url, {
        params: data
      })
    }else{
      promise = axios.post(url, data)
    }

    /*promise.then((response)=>{
      resolve(response.data)
    }).catch((error)=>{
      alert("Request Failed" + error)
    })*/

    promise.then(response => { // 请求成功, 调用resolve(response.data)
      resolve(response.data)
    }).catch(error => {  // 请求出错了, 不调用reject(), 直接提示(外部不需要处理异常)
      alert('请求出错: ' + error.message)
    })
  })
}
